package com.test.daily.leetcode.y2022.m06.day0608.v02;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/6/8 下午 04:00
 * @version: 1.0
 */
public class Solution {
    public static int convertCount(String s) {
        if (s == null && s.length() == 0) {
            return 0;
        }
        char[] str = s.toCharArray();
        return process(str, 0);
    }

    public static int process(char[] str, int index) {
        if (index == str.length) {
            return 1;
        }
        if (str[index] == '0') {
            return 0;
        }
        int ways = process(str, index + 1);
        if (index + 1 < str.length && (str[index] - '0') * 10 + (str[index] - '0') < 27) {
            ways += process(str, index + 2);
        }
        return ways;
    }

    public static int convertCount1(String s) {
        char[] str = s.toCharArray();
        int[] dp = new int[s.length()];

        dp[str.length] = 1;
        for (int i = str.length - 1; i >= 0; i--) {
            if (str[i] != '0') {
                dp[i] = dp[i + 1];
                if (i + 1 < str.length && (str[i] - '0') * 10 + str[i + 1] < 27) {
                    dp[i] += dp[i + 2];
                }
            }
        }
        return dp[0];
    }

    public static int convertCount3(String s) {
        char[] str = s.toCharArray();
        int N = str.length;
        int[] dp = new int[N+1];
        str[N] = 1;
        int ans = 0;
        for(int i = N - 1; i >= 0; i--){
            if(str[i] != '0'){
                ans = dp[i+1];
                if(i + 1 < N){
                    ans += dp[i+2];
                }
            }
            dp[i] = ans;
        }
        return dp[0];
    }
    public static int convertDp(String s){
        if(s == null || s.length() == 0 || s.charAt(0) == '0'){
            return 0;
        }
        char[] str = s.toCharArray();
        int N = str.length;
        int[] dp = new int[N+1];

        dp[0] = 1;
        for(int i = 1; i < N; i++){
            if(str[i] == '0'){
                if(str[i] == '0' || str[i] - '0' > 2 || (i -2 > 0 && dp[i-2] == 0)){
                    return 0;
                } else {
                    dp[i] = dp[i-2] > 0 ? dp[i-2]: 1;
                }
            } else {
                dp[i] = dp[i-1];
                if(str[i-1] != '0' && (str[i-1] - '0') * 10 + str[i] - '0' < 27){
                    dp[i] += dp[i-2];
                }
            }
        }

        return dp[N-1];
    }
}
